/**
 * Imported packages
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @authors BENSON WACHIRA, DOROTHY SARPONG, STANLEY ANKU
 * MICHAEL ESHUN & MAKAFUI AMEZAH
 */

/**
 * 
 * A Controller to modify flight's data
 */
public class ControllerModifyFlight {
    
    /**
     * Objects and variable declarations
     */
    Connection conn = null;
    String querry;
    Statement st = null;
    public ModifyFlight modFlt;
    private ActionListener actionListener;
    
    
    /**
     * Constructor
     */
    public ControllerModifyFlight() {
        modFlt = new ModifyFlight();
        modFlt.setVisible(true);

    }
       
    /**
     * Method to update flight's data
     */
    public void setList() {
        String ky = "";
        ky = modFlt.getKey().getText();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull", "root", "");
            querry = "update flights set fName =?,ffrom=?,destination=?,Price=?,fCapacity=?,depatureTime=?,arrivalTime=?"
                    + "where fName like '%" + ky + "%'";
            PreparedStatement stmt = conn.prepareStatement(querry);
        //st.execute("USE jetwingdb;");

            stmt.setString(1, modFlt.getFname().getText());
            stmt.setString(2, modFlt.getFromtext().getText());
            stmt.setString(3, modFlt.getDesttext().getText());
            stmt.setString(4, modFlt.getCosttext().getText());
            stmt.setString(5, modFlt.getCaptext().getText());
            stmt.setString(6, modFlt.getDepttext().getText());
            stmt.setString(7, modFlt.getArivtext().getText());
            stmt.execute();
            conn.close();
            JOptionPane.showMessageDialog(null, "Flight modified succesfully");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

    }
    
    /**
     * Method to display flights in database
     */
    public void listFlights() {

        String ky = "";
        ky = modFlt.getKey().getText();
        System.out.println(ky);
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull", "root", "");
            querry = "SELECT * FROM Flights where fName like '%" + ky + "%'";
            st = conn.prepareStatement(querry);
            st.execute("USE jetwingdb;");

            ResultSet rs = st.executeQuery(querry);
            while (rs.next()) {

                modFlt.getFname().setText(rs.getString("fName"));
                modFlt.getFromtext().setText(rs.getString("ffrom"));
                modFlt.getDesttext().setText(rs.getString("destination"));
                modFlt.getCosttext().setText(rs.getString("Price"));
                modFlt.getCaptext().setText(rs.getString("fCapacity"));
                modFlt.getDepttext().setText(rs.getString("depatureTime"));
                modFlt.getArivtext().setText(rs.getString("arrivalTime"));
                System.out.println("Hey: " + rs.getString("fName"));
            }
            //st.close();
           // conn.close();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

    }
    
    /**
     * Method to get source of button clicked using action listener
     */
    public void control() {
        actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                if (actionEvent.getSource() == modFlt.getSaveChanges()) {
                    setList();
                }
                if (actionEvent.getSource() == modFlt.getClose()) {
                    modFlt.setVisible(false);

                }
                if (actionEvent.getSource() == modFlt.getSearchButton()) {
                    listFlights();
                }
            }

        };
        modFlt.getSearchButton().addActionListener(actionListener);
        modFlt.getClose().addActionListener(actionListener);
        modFlt.getSaveChanges().addActionListener(actionListener);
    }

    

}
